<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>11.5. filecmp — File and Directory Comparisons &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="11. File and Directory Access" href="filesys.html" />
    <link rel="next" title="11.6. tempfile — Generate temporary files and directories" href="tempfile.html" />
    <link rel="prev" title="11.4. stat — Interpreting stat() results" href="stat.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tempfile.html" title="11.6. tempfile — Generate temporary files and directories"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="stat.html" title="11.4. stat — Interpreting stat() results"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="filesys.html" accesskey="U">11. File and Directory Access</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-filecmp">
<span id="filecmp-file-and-directory-comparisons"></span><h1>11.5. <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></a> &#8212; File and Directory Comparisons<a class="headerlink" href="#module-filecmp" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.4/Lib/filecmp.py">Lib/filecmp.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></a> module defines functions to compare files and directories,
with various optional time/correctness trade-offs. For comparing files,
see also the <a class="reference internal" href="difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> module.</p>
<p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></a> module defines the following functions:</p>
<dl class="function">
<dt id="filecmp.cmp">
<tt class="descclassname">filecmp.</tt><tt class="descname">cmp</tt><big>(</big><em>f1</em>, <em>f2</em>, <em>shallow=True</em><big>)</big><a class="headerlink" href="#filecmp.cmp" title="Permalink to this definition">¶</a></dt>
<dd><p>Compare the files named <em>f1</em> and <em>f2</em>, returning <tt class="docutils literal"><span class="pre">True</span></tt> if they seem equal,
<tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p>
<p>If <em>shallow</em> is true, files with identical <a class="reference internal" href="os.html#os.stat" title="os.stat"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> signatures are
taken to be equal.  Otherwise, the contents of the files are compared.</p>
<p>Note that no external programs are called from this function, giving it
portability and efficiency.</p>
<p>This function uses a cache for past comparisons and the results,
with cache entries invalidated if the <a class="reference internal" href="os.html#os.stat" title="os.stat"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> information for the
file changes.  The entire cache may be cleared using <a class="reference internal" href="#filecmp.clear_cache" title="filecmp.clear_cache"><tt class="xref py py-func docutils literal"><span class="pre">clear_cache()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="filecmp.cmpfiles">
<tt class="descclassname">filecmp.</tt><tt class="descname">cmpfiles</tt><big>(</big><em>dir1</em>, <em>dir2</em>, <em>common</em>, <em>shallow=True</em><big>)</big><a class="headerlink" href="#filecmp.cmpfiles" title="Permalink to this definition">¶</a></dt>
<dd><p>Compare the files in the two directories <em>dir1</em> and <em>dir2</em> whose names are
given by <em>common</em>.</p>
<p>Returns three lists of file names: <em>match</em>, <em>mismatch</em>,
<em>errors</em>.  <em>match</em> contains the list of files that match, <em>mismatch</em> contains
the names of those that don&#8217;t, and <em>errors</em> lists the names of files which
could not be compared.  Files are listed in <em>errors</em> if they don&#8217;t exist in
one of the directories, the user lacks permission to read them or if the
comparison could not be done for some other reason.</p>
<p>The <em>shallow</em> parameter has the same meaning and default value as for
<a class="reference internal" href="#filecmp.cmp" title="filecmp.cmp"><tt class="xref py py-func docutils literal"><span class="pre">filecmp.cmp()</span></tt></a>.</p>
<p>For example, <tt class="docutils literal"><span class="pre">cmpfiles('a',</span> <span class="pre">'b',</span> <span class="pre">['c',</span> <span class="pre">'d/e'])</span></tt> will compare <tt class="docutils literal"><span class="pre">a/c</span></tt> with
<tt class="docutils literal"><span class="pre">b/c</span></tt> and <tt class="docutils literal"><span class="pre">a/d/e</span></tt> with <tt class="docutils literal"><span class="pre">b/d/e</span></tt>.  <tt class="docutils literal"><span class="pre">'c'</span></tt> and <tt class="docutils literal"><span class="pre">'d/e'</span></tt> will each be in
one of the three returned lists.</p>
</dd></dl>

<dl class="function">
<dt id="filecmp.clear_cache">
<tt class="descclassname">filecmp.</tt><tt class="descname">clear_cache</tt><big>(</big><big>)</big><a class="headerlink" href="#filecmp.clear_cache" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the filecmp cache. This may be useful if a file is compared so quickly
after it is modified that it is within the mtime resolution of
the underlying filesystem.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<div class="section" id="the-dircmp-class">
<span id="dircmp-objects"></span><h2>11.5.1. The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class<a class="headerlink" href="#the-dircmp-class" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="filecmp.dircmp">
<em class="property">class </em><tt class="descclassname">filecmp.</tt><tt class="descname">dircmp</tt><big>(</big><em>a</em>, <em>b</em>, <em>ignore=None</em>, <em>hide=None</em><big>)</big><a class="headerlink" href="#filecmp.dircmp" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new directory comparison object, to compare the directories <em>a</em>
and <em>b</em>.  <em>ignore</em> is a list of names to ignore, and defaults to
<a class="reference internal" href="#filecmp.DEFAULT_IGNORES" title="filecmp.DEFAULT_IGNORES"><tt class="xref py py-attr docutils literal"><span class="pre">filecmp.DEFAULT_IGNORES</span></tt></a>.  <em>hide</em> is a list of names to hide, and
defaults to <tt class="docutils literal"><span class="pre">[os.curdir,</span> <span class="pre">os.pardir]</span></tt>.</p>
<p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class compares files by doing <em>shallow</em> comparisons
as described for <a class="reference internal" href="#filecmp.cmp" title="filecmp.cmp"><tt class="xref py py-func docutils literal"><span class="pre">filecmp.cmp()</span></tt></a>.</p>
<p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class provides the following methods:</p>
<dl class="method">
<dt id="filecmp.dircmp.report">
<tt class="descname">report</tt><big>(</big><big>)</big><a class="headerlink" href="#filecmp.dircmp.report" title="Permalink to this definition">¶</a></dt>
<dd><p>Print (to <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><tt class="xref py py-data docutils literal"><span class="pre">sys.stdout</span></tt></a>) a comparison between <em>a</em> and <em>b</em>.</p>
</dd></dl>

<dl class="method">
<dt id="filecmp.dircmp.report_partial_closure">
<tt class="descname">report_partial_closure</tt><big>(</big><big>)</big><a class="headerlink" href="#filecmp.dircmp.report_partial_closure" title="Permalink to this definition">¶</a></dt>
<dd><p>Print a comparison between <em>a</em> and <em>b</em> and common immediate
subdirectories.</p>
</dd></dl>

<dl class="method">
<dt id="filecmp.dircmp.report_full_closure">
<tt class="descname">report_full_closure</tt><big>(</big><big>)</big><a class="headerlink" href="#filecmp.dircmp.report_full_closure" title="Permalink to this definition">¶</a></dt>
<dd><p>Print a comparison between <em>a</em> and <em>b</em> and common subdirectories
(recursively).</p>
</dd></dl>

<p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class offers a number of interesting attributes that may be
used to get various bits of information about the directory trees being
compared.</p>
<p>Note that via <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> hooks, all attributes are computed lazily,
so there is no speed penalty if only those attributes which are lightweight
to compute are used.</p>
<dl class="attribute">
<dt id="filecmp.dircmp.left">
<tt class="descname">left</tt><a class="headerlink" href="#filecmp.dircmp.left" title="Permalink to this definition">¶</a></dt>
<dd><p>The directory <em>a</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.right">
<tt class="descname">right</tt><a class="headerlink" href="#filecmp.dircmp.right" title="Permalink to this definition">¶</a></dt>
<dd><p>The directory <em>b</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.left_list">
<tt class="descname">left_list</tt><a class="headerlink" href="#filecmp.dircmp.left_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Files and subdirectories in <em>a</em>, filtered by <em>hide</em> and <em>ignore</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.right_list">
<tt class="descname">right_list</tt><a class="headerlink" href="#filecmp.dircmp.right_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Files and subdirectories in <em>b</em>, filtered by <em>hide</em> and <em>ignore</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.common">
<tt class="descname">common</tt><a class="headerlink" href="#filecmp.dircmp.common" title="Permalink to this definition">¶</a></dt>
<dd><p>Files and subdirectories in both <em>a</em> and <em>b</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.left_only">
<tt class="descname">left_only</tt><a class="headerlink" href="#filecmp.dircmp.left_only" title="Permalink to this definition">¶</a></dt>
<dd><p>Files and subdirectories only in <em>a</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.right_only">
<tt class="descname">right_only</tt><a class="headerlink" href="#filecmp.dircmp.right_only" title="Permalink to this definition">¶</a></dt>
<dd><p>Files and subdirectories only in <em>b</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.common_dirs">
<tt class="descname">common_dirs</tt><a class="headerlink" href="#filecmp.dircmp.common_dirs" title="Permalink to this definition">¶</a></dt>
<dd><p>Subdirectories in both <em>a</em> and <em>b</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.common_files">
<tt class="descname">common_files</tt><a class="headerlink" href="#filecmp.dircmp.common_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Files in both <em>a</em> and <em>b</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.common_funny">
<tt class="descname">common_funny</tt><a class="headerlink" href="#filecmp.dircmp.common_funny" title="Permalink to this definition">¶</a></dt>
<dd><p>Names in both <em>a</em> and <em>b</em>, such that the type differs between the
directories, or names for which <a class="reference internal" href="os.html#os.stat" title="os.stat"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> reports an error.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.same_files">
<tt class="descname">same_files</tt><a class="headerlink" href="#filecmp.dircmp.same_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Files which are identical in both <em>a</em> and <em>b</em>, using the class&#8217;s
file comparison operator.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.diff_files">
<tt class="descname">diff_files</tt><a class="headerlink" href="#filecmp.dircmp.diff_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Files which are in both <em>a</em> and <em>b</em>, whose contents differ according
to the class&#8217;s file comparison operator.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.funny_files">
<tt class="descname">funny_files</tt><a class="headerlink" href="#filecmp.dircmp.funny_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Files which are in both <em>a</em> and <em>b</em>, but could not be compared.</p>
</dd></dl>

<dl class="attribute">
<dt id="filecmp.dircmp.subdirs">
<tt class="descname">subdirs</tt><a class="headerlink" href="#filecmp.dircmp.subdirs" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary mapping names in <a class="reference internal" href="#filecmp.dircmp.common_dirs" title="filecmp.dircmp.common_dirs"><tt class="xref py py-attr docutils literal"><span class="pre">common_dirs</span></tt></a> to <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a>
objects.</p>
</dd></dl>

</dd></dl>

<dl class="attribute">
<dt id="filecmp.DEFAULT_IGNORES">
<tt class="descclassname">filecmp.</tt><tt class="descname">DEFAULT_IGNORES</tt><a class="headerlink" href="#filecmp.DEFAULT_IGNORES" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
<p>List of directories ignored by <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> by default.</p>
</dd></dl>

<p>Here is a simplified example of using the <tt class="docutils literal"><span class="pre">subdirs</span></tt> attribute to search
recursively through two directories to show common different files:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">filecmp</span> <span class="k">import</span> <span class="n">dircmp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">print_diff_files</span><span class="p">(</span><span class="n">dcmp</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">diff_files</span><span class="p">:</span>
<span class="gp">... </span>        <span class="nb">print</span><span class="p">(</span><span class="s">&quot;diff_file %s found in %s and %s&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">left</span><span class="p">,</span>
<span class="gp">... </span>              <span class="n">dcmp</span><span class="o">.</span><span class="n">right</span><span class="p">))</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">sub_dcmp</span> <span class="ow">in</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">subdirs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
<span class="gp">... </span>        <span class="n">print_diff_files</span><span class="p">(</span><span class="n">sub_dcmp</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dcmp</span> <span class="o">=</span> <span class="n">dircmp</span><span class="p">(</span><span class="s">&#39;dir1&#39;</span><span class="p">,</span> <span class="s">&#39;dir2&#39;</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">print_diff_files</span><span class="p">(</span><span class="n">dcmp</span><span class="p">)</span> 
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">11.5. <tt class="docutils literal"><span class="pre">filecmp</span></tt> &#8212; File and Directory Comparisons</a><ul>
<li><a class="reference internal" href="#the-dircmp-class">11.5.1. The <tt class="docutils literal"><span class="pre">dircmp</span></tt> class</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="stat.html"
                        title="previous chapter">11.4. <tt class="docutils literal"><span class="pre">stat</span></tt> &#8212; Interpreting <tt class="docutils literal"><span class="pre">stat()</span></tt> results</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="tempfile.html"
                        title="next chapter">11.6. <tt class="docutils literal"><span class="pre">tempfile</span></tt> &#8212; Generate temporary files and directories</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/filecmp.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tempfile.html" title="11.6. tempfile — Generate temporary files and directories"
             >next</a> |</li>
        <li class="right" >
          <a href="stat.html" title="11.4. stat — Interpreting stat() results"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="filesys.html" >11. File and Directory Access</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Feb 26, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>